package com.songsong.myblog2.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.songsong.myblog2.domain.Article;
import com.songsong.myblog2.domain.Photo;
import com.songsong.myblog2.domain.User;
import com.songsong.myblog2.domain.UserImage;
import com.songsong.myblog2.mapper.UserMapper;
import com.songsong.myblog2.service.UserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.songsong.myblog2.util.TimeUtil;

import java.util.List;

/**
 * Created by Administrator on 2018/12/24 0024.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //通过用户手机号查找用户对象
    @Override
    public User findUser(String username) {
        return userMapper.findUser(username);
    }

    /*注册用户*/
    @Override
    public String addUser(User user) {

        user.setUsername0(user.getUsername0().trim().replaceAll(" ", ""));
        String username0 = user.getUsername0();

        if (username0.length() > 35 || "".equals(username0)) {
            return "4";
        }
        User user1 = userMapper.findUser(user.getUsername());
        if (user1 != null) {
            return "1";
        }
        user.setRole("user");
        userMapper.addUser(user);
        //添加用户默认头像
        Photo photo = new Photo();
        photo.setUserphone(user.getUsername());
        photo.setUserurl("http://songhangfei.oss-cn-beijing.aliyuncs.com/songsong/userPhoto/15937576731/1552035813691.jpg");
        photo.setUserdate(new TimeUtil().getFormatDateForThree());
        userMapper.insertUserPhoto(photo);
        return "2";
    }

    /*添加用户图片*/
    @Override
    public void addUserIamge(UserImage userImage) {
        userMapper.addUserImage(userImage);
    }

    /*获取用户图片*/
    @Override
    public List<UserImage> getUserImage(String username) {
        List<UserImage> userImage = userMapper.getUserImage(username);
        return userImage;
    }

    /*批量删除用户图片*/
    @Override
    public void deleteUserImage(Object[] imageId) {
        userMapper.deleteUserImage(imageId);
    }

    /*单个删除用户图片*/
    @Override
    public void deleteUserImageAlone(String userImageId) {
        userMapper.deleteUserImageAlone(userImageId);
    }

    @Override
    public String getUserPhoto(String userphone) {
        return userMapper.getUserPhoto(userphone);
    }

    @Override
    public void updateUserHead(Photo photo) {
        userMapper.updateUserHead(photo);
    }

    @Override
    public JSONObject getUserPersonalInfoByUsername(String userphone) {
        User user = userMapper.getUserPersonalInfoByUsername(userphone);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 200);
        JSONObject userJon = new JSONObject();
        userJon.put("phone", user.getUsername());
        userJon.put("username", user.getNickName());
        userJon.put("gender", user.getSex());
        userJon.put("trueName", user.getUsername0());
        userJon.put("birthday", user.getBirthday());
        userJon.put("email", user.getEmail());
        userJon.put("personalBrief", user.getSignature());
        jsonObject.put("result", userJon);
        return jsonObject;
    }

    @Override
    public JSONObject savePersonalDate(User user) {

        JSONObject returnJson = new JSONObject();
        returnJson.put("status", 200);
        userMapper.savePersonalDate(user);
        return returnJson;
    }

    //获取用户管理
    @Override
    public JSONObject getUserManagement(int rows, int pageNum) {
        PageHelper.startPage(pageNum, rows);
        List<User> users = userMapper.getUserManagement();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        JSONArray returnJsonArray = new JSONArray();
        JSONObject returnJson = new JSONObject();
        JSONObject userJson;
        for (User user : users) {
            userJson = new JSONObject();
            userJson.put("username0", user.getUsername0());
            userJson.put("birthday", user.getBirthday());
            userJson.put("userphone", user.getUsername());
            userJson.put("role", user.getRole());
            returnJsonArray.add(userJson);
        }
        returnJson.put("status", 200);
        returnJson.put("result", returnJsonArray);
        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum", pageInfo.getPageNum());
        pageJson.put("pageSize", pageInfo.getPageSize());
        pageJson.put("total", pageInfo.getTotal());
        pageJson.put("pages", pageInfo.getPages());
        pageJson.put("isFirstPage", pageInfo.isIsFirstPage());
        pageJson.put("isLastPage", pageInfo.isIsLastPage());
        returnJson.put("pageInfo", pageJson);
        return returnJson;
    }

    @Override
    public void updateUserToAdmin(String userPhone) {
        userMapper.updateUserToAdmin(userPhone);
    }

    @Override
    public void updateAdminToUser(String userPhone) {
        userMapper.updateAdminToUser(userPhone);
    }

    @Override
    public void deleteUser(String userPhone) {
        userMapper.deteUser(userPhone);
    }

    @Override
    public void updatePasswordByPhone(String phone, String password, String salt) {
        userMapper.updatePassword(phone, password, salt);
    }

    @Override
    public int getUserNum() {
        return userMapper.getUserNum();
    }
}
